﻿using System.Collections.Generic;
using System.Linq;

namespace SeaBattle.Domain.Entities
{
    public class Fleet
    {
        private readonly byte _maxShipCount;
        private readonly byte _maxPartCount;

        private readonly List<Ship> _ships = new List<Ship>();

        public Fleet(byte maxShipCount, byte maxPartCount)
        {
            _maxShipCount = maxShipCount;
            _maxPartCount = maxPartCount;
        }

        public int Id { get; set; }

        private bool CanAddShip(Ship ship)
        {
            if (_ships.Count + 1 > _maxShipCount)
                return false;
            var count = _ships.Sum(x => x.PartsCount);
            return count + ship.PartsCount <= _maxPartCount;
        }

        public void AddShip(Ship ship)
        {
            if (CanAddShip(ship))
            {
                _ships.Add(ship);
            }
        }

        public void RemoveShip(Ship ship)
        {
            _ships.Remove(ship);
        }

    }
}
